package com.shop.service;

import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.UUID;

import org.springframework.transaction.annotation.Transactional;

import com.shop.pojo.Advice;
import com.shop.pojo.Orderlist;
import com.shop.pojo.Orders;
import com.shop.pojo.Users;
@Transactional
public class UserService extends SysService {


	public Users login(Users user) {

		@SuppressWarnings("unchecked")
		List<Users> list = getUserDao().find(
				"From Users user where user.uname=? and user.upassword=?",
				new Object[] { user.getUname(), user.getUpassword() });

		return list != null && list.size() > 0 ? list.get(0) : null;

	}

	
	public boolean register(Users user) {
		user.setIdUser(UUID.randomUUID().toString());
		user.setUno("u_" + UUID.randomUUID().toString());
		if (getUserDao().save(user) != null) {
			return true;
		}
		return false;
	}

	@Transactional
	public boolean checked(String param) {
		List<Users> users = getUserDao().find(
				"from Users user where user.uname=?", new Object[] { param });
		if (users != null && users.size() > 0) {
			return false;
		}
		return true;
	}
	@Transactional
	public boolean advice(Advice advice) {
		advice.setIdAdvice(UUID.randomUUID().toString());
		advice.setAdviceno("adv_"+UUID.randomUUID().toString());
		if(getAdviceDao().save(advice) !=null){
			return true;		
		}
		return false;
	}

	public boolean mkorder(Users user, Set<Orderlist> car, Orders order) {

			order.setIdOrder(UUID.randomUUID().toString());
			order.setOno("ono" + UUID.randomUUID().toString());
			order.setOdate(new Date());
			order.setOrderlists(car);
			System.out.println(user.getIdUser());
			order.setUsers(user);
			getOrdersDao().save(order);
			return true;
		

	}
}
